package com.devilvan.betrice.mg.controller;

import com.alibaba.fastjson.JSONObject;
import com.devilvan.betrice.common.BetriceResultVo;
import com.devilvan.betrice.exception.BetriceException;
import com.devilvan.betrice.exception.constant.BetriceExceptionConstant;
import com.devilvan.betrice.mg.po.MgDetailPo;
import com.devilvan.betrice.mg.service.MgDetailService;
import com.devilvan.betrice.mg.vo.MgDetailRequestVo;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
 * @author Evad.Wu
 * @Description 198763-明细信息表 控制器
 * @date 2023-01-26
 */
@RestController
@RequestMapping(value = "mgDetailController")
@Tag(name = "198763-明细信息表 控制器")
public class MgDetailController {
    @Resource(name = "mgDetailServiceImpl")
    private MgDetailService mgDetailService;

    /**
     * 总请求控制器
     *
     * @return 形式返回
     */
    @Operation(summary = "总请求控制器")
    @GetMapping(value = "doMgDetail")
    public BetriceResultVo<MgDetailPo> doMgDetail() {
        BetriceResultVo<MgDetailPo> betriceResultVo = new BetriceResultVo<>();
        return betriceResultVo;
    }

    /**
     * 采集报文控制器
     *
     * @param request 报文 {@link MgDetailRequestVo}
     * @return 形式返回
     */
    @Operation(summary = "采集报文控制器")
    @GetMapping(value = "doMgDetailCrawler")
    public BetriceResultVo<MgDetailPo> doMgDetailCrawler(@RequestBody String request) {
        MgDetailRequestVo mgDetailRequestVo = JSONObject.parseObject(request, MgDetailRequestVo.class);
        return mgDetailService.doMgDetailCrawler(mgDetailRequestVo);
    }


    /**
     * MgDetail redis队列消费者
     *
     * @param type 类型
     * @return 形式返回
     */
    @Operation(summary = "MgDetail redis队列消费者")
    @GetMapping(value = "doMgDetailConsumer")
    public BetriceResultVo<Object> doMgDetailConsumer(@RequestParam String type, @RequestParam Integer pageSize) {
        return mgDetailService.doMgDetailConsumer(type, pageSize);
    }

    /**
     * MgDetail redis队列消费者
     *
     * @param type 类型
     * @return 形式返回
     */
    @Async(value = "threadPoolExecutor")
    @XxlJob(value = "doMgDetailScheduler")
    @Operation(summary = "MgDetail redis队列消费者")
    @GetMapping(value = "doMgDetailScheduler")
    public CompletableFuture<BetriceResultVo<Object>> doMgDetailScheduler() {
        String param = Optional.ofNullable(XxlJobHelper.getJobParam())
                .orElseThrow(() -> new BetriceException(BetriceExceptionConstant.JOB_PARAMS_NOT_NULL_EXP.getMessage()));
        String[] spl = param.split(",");
        return CompletableFuture.completedFuture(mgDetailService.doMgDetailConsumer(spl[0], Integer.valueOf(spl[1])));
    }

    /**
     * 解析报文控制器
     *
     * @param request 报文
     * @return 形式返回
     */
    @Operation(summary = "解析报文控制器")
    @GetMapping(value = "doMgDetailRequest")
    public BetriceResultVo<MgDetailPo> doMgDetailRequest(@RequestBody String request) {
        BetriceResultVo<MgDetailPo> betriceResultVo = new BetriceResultVo<>();
        return betriceResultVo;
    }
}
